﻿package {
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.events.ProgressEvent;
	import flash.events.MouseEvent;

	public class KeyboardDemo extends Sprite {
		private var step:uint=2;
		private var moveWait:int=5;
		private var jumpWait:int=2;
		private var pickHydrantX:int=0;
		private var pickHydrantY:int=0;
		private var hydrantStartY:int=470;
		private var dudeY:int=460;
		private var superSpeed:Boolean=false;
		private var invisible:Boolean=false;
		private var liftedHydrant:Boolean=false;
		private var facingRight:Boolean=true;
		private var moving:Boolean=false;
		private var hydrantGravity:Number=.01;
		private var hydrantChangeInY:Number=-.5;
		private var hydrantThrow:Number=3;
		private var jumpHeight:Number=-6;// larger negatives = higher jumps
		private var gravity:Number=.07;// bigger number = more gravity
		private var leftTimer:Timer=new Timer(moveWait,1);
		private var rightTimer:Timer=new Timer(moveWait,1);
		private var shootLeftTimer:Timer=new Timer(1,1);
		private var shootRightTimer:Timer=new Timer(1,1);
		private var jumpTimer:Timer=new Timer(jumpWait,1);
		private var pickupHydrantTimer:Timer=new Timer(jumpWait,1);
		private var throwHydrantLeftTimer:Timer=new Timer(jumpWait,1);
		private var throwHydrantRightTimer:Timer=new Timer(jumpWait,1);
		private var changeAlphaTimer:Timer=new Timer(jumpWait,1);
		private var key:uint=0;
		private var dudeMove:Number=0;
		private var backgroundMove:Number=0;
		private var enemyHealth:Number=100;

		/*public function startLoad(event:MouseEvent) {
		
		var mLoader:Loader = new Loader();
		var mRequest:URLRequest=new URLRequest("flash-databasetest.swf");
		mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
		mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
		mLoader.load(mRequest);
		}
		public function onCompleteHandler(loadEvent:Event) {
		addChild(loadEvent.currentTarget.content);
		}
		public function onProgressHandler(mProgress:ProgressEvent) {
		var percent:Number=mProgress.bytesLoaded/mProgress.bytesTotal;
		trace(percent);
		}
		*/
		public function KeyboardDemo() {
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
			
			leftTimer.addEventListener(TimerEvent.TIMER, moveLeft);
			rightTimer.addEventListener(TimerEvent.TIMER, moveRight);
			shootLeftTimer.addEventListener(TimerEvent.TIMER, shootFireLeft);
			shootRightTimer.addEventListener(TimerEvent.TIMER, shootFireRight);
			jumpTimer.addEventListener(TimerEvent.TIMER, jump);
			pickupHydrantTimer.addEventListener(TimerEvent.TIMER, pickupHydrant);
			throwHydrantLeftTimer.addEventListener(TimerEvent.TIMER, throwHydrantLeft);
			throwHydrantRightTimer.addEventListener(TimerEvent.TIMER, throwHydrantRight);
			changeAlphaTimer.addEventListener(TimerEvent.TIMER, changeAlpha);

			addEventListener(Event.ENTER_FRAME, testMovement);

			//hydrant.addEventListener(MouseEvent.MOUSE_UP, startLoad);

			dudeRight.width=82;
			dudeRight.height=126;
			dudeRight.x=-500;
			dude.y=dudeY;
			pickHydrantX=hydrant.width/2-dude.width/2;
			pickHydrantY=dude.height-50;
			evilHealthTxt.text="Evil Dude Health 100%";
		}

		private function keyPressedDown(event:KeyboardEvent):void {
			key=event.keyCode;
			switch (key) {
				case Keyboard.LEFT :
					facingRight=false;
					leftTimer.start();
					break;
				case Keyboard.RIGHT :
					facingRight=true;
					rightTimer.start();
					break;
				case Keyboard.UP :
					jumpTimer.start();
					break;
				case Keyboard.CONTROL :
					if (superSpeed) {
						superSpeed=false;
						step=2;
					} else {
						superSpeed=true;
						step=7;
					}
					break;
				case Keyboard.SHIFT :
					changeAlphaTimer.start();
					break;
				case 65 :
					if (dude.hitTestObject(hydrant)) {
						if (liftedHydrant) {
							if (facingRight) {
								throwHydrantLeftTimer.reset();
								throwHydrantRightTimer.start();
								hydrantThrow=3;
							} else {
								throwHydrantRightTimer.reset();
								throwHydrantLeftTimer.start();
								hydrantThrow=-3;
							}
							liftedHydrant=false;
						} else {
							if (hydrant.y>=hydrantStartY&&dude.y==dudeY) {
								pickupHydrantTimer.start();
								liftedHydrant=true;
							}
						}
					}
					break;
				case Keyboard.SPACE :
					if (fireball.x<=0||fireball.x>800) {
						if (facingRight) {
							shootLeftTimer.reset();
							fireball.x=dude.x+40;
							fireball.y=dude.y+40;
							shootRightTimer.start();
						} else {
							shootRightTimer.reset();
							fireball.x=dude.x;
							fireball.y=dude.y+40;
							shootLeftTimer.start();
						}
						break;
					}
			}
		}

		private function keyReleased(event:KeyboardEvent):void {
			var key:uint=event.keyCode;
			switch (key) {
				case Keyboard.LEFT :
					leftTimer.reset();
					break;
				case Keyboard.RIGHT :
					rightTimer.reset();
					break;
			}
		}
		
		public function testMovement(event:Event) {
			if (dudeMove!=dude.x||backgroundMove!=background.x) {
				if (facingRight) {
					dudeRight.x=dude.x;
					dudeRight.y=dude.y;
					dudeRight.alpha=dude.alpha;
					dude.visible=false;
					dude=dudeRight;
					dude.visible=true;
				} else {

					dudeLeft.x=dude.x;
					dudeLeft.y=dude.y;
					dudeLeft.alpha=dude.alpha;
					dude.visible=false;
					dude=dudeLeft;
					dude.visible=true;
				}
			} else {
				if (facingRight) {
					dudeStandRight.x=dude.x;
					dudeStandRight.y=dude.y;
					dudeStandRight.alpha=dude.alpha;
					dude.visible=false;
					dude=dudeStandRight;
					dude.visible=true;
				} else {
					dudeStandLeft.x=dude.x;
					dudeStandLeft.y=dude.y;
					dudeStandLeft.alpha=dude.alpha;
					dude.visible=false;
					dude=dudeStandLeft;
					dude.visible=true;
				}
			}
			dudeMove=dude.x;
			backgroundMove=background.x;
		}
		
		private function moveLeft(event:TimerEvent):void {
			dude.x-=step;
			if (dude.x<250) {
				dude.x+=step;
				if (background.x<0) {
					background.x+=step;
					hydrant.x+=step;
				} else {
					if (dude.x>0) {
						dude.x-=step;
					}
				}
			}
			if (liftedHydrant) {
				hydrant.x=dude.x-pickHydrantX;
			}
			evilDude.x=background.x+1500;
			leftTimer.reset();
			leftTimer.start();
		}

		private function moveRight(event:TimerEvent):void {
			dude.x+=step;
			if (dude.x>500) {
				dude.x-=step;
				if (background.x>-(background.width-800)) {
					background.x-=step;
					hydrant.x-=step;
				} else {
					if (dude.x<(800-dude.width)) {
						dude.x+=step;
					}
				}
			}
			if (liftedHydrant) {
				hydrant.x=dude.x-pickHydrantX;
			}
			evilDude.x=background.x+1500;
			rightTimer.reset();
			rightTimer.start();
		}

		private function shootFireLeft(event:TimerEvent):void {
			fireball.x-=2;
			fireball.rotation+=2;
			if (fireball.hitTestObject(evilDude)) {
				fireball.y=-30;
				hitEnemy(10);
			}
			shootLeftTimer.reset();
			shootLeftTimer.start();
		}

		private function shootFireRight(event:TimerEvent):void {
			fireball.x+=2;
			fireball.rotation+=2;
			if (fireball.hitTestObject(evilDude)) {
				fireball.y=-30;
				hitEnemy(10);
			}
			shootRightTimer.reset();
			shootRightTimer.start();
		}
		
		private function hitEnemy(damage:int):void {
			enemyHealth-=damage;
			evilHealthTxt.text="Evil Dude Health " + enemyHealth + "%";
		}

		private function jumpUp():void {
			if (dude.y<=dudeY) {
				jumpHeight+=gravity;
				dude.y+=jumpHeight;
				if (liftedHydrant) {
					hydrant.y=dude.y-pickHydrantY;
				}
				jumpTimer.start();
			}
		}

		private function jump(event:TimerEvent):void {
			if (dude.y>dudeY) {
				dude.y=dudeY;
				jumpHeight=-6;// jumpHeight has to be reset
			} else {
				jumpTimer.reset();
				jumpUp();
			}
		}

		private function pickupHydrant(event:TimerEvent):void {
			if (hydrant.y>dude.y-pickHydrantY) {
				hydrant.y--;
				if (hydrant.x+hydrant.width/2>dude.x+dude.width/2) {
					hydrant.x--;
				} else {
					hydrant.x++;
				}
				pickupHydrantTimer.reset();
				pickupHydrantTimer.start();
			}
		}

		private function throwHydrantLeft(event:TimerEvent):void {
			if (hydrant.y<hydrantStartY) {
				hydrantChangeInY+=hydrantGravity;
				hydrant.y+=hydrantChangeInY;
				if (hydrant.x>background.x) {
					hydrant.x+=hydrantThrow;
				}
				else
				{	
					hydrant.x++;
					hydrantThrow=-hydrantThrow;
				}
				throwHydrantLeftTimer.reset();
				throwHydrantLeftTimer.start();
			} else {
				hydrantChangeInY=-.5;
			}
			if (hydrant.hitTestObject(evilDude) && hydrant.x>evilDude.x && hydrant.x<(evilDude.x+evilDude.width/10))
			{
				hitEnemy(50);
				hydrant.x+=20;
				hydrantThrow=0;
			}
		}

		private function throwHydrantRight(event:TimerEvent):void {
			if (hydrant.y<hydrantStartY) {
				hydrantChangeInY+=hydrantGravity;
				hydrant.y+=hydrantChangeInY;
				if ( hydrant.x < ( background.width + background.x - hydrant.width )) {
					hydrant.x+=hydrantThrow;
				}
				else
				{	
					hydrant.x--;
					hydrantThrow=-hydrantThrow;
				}
				throwHydrantRightTimer.reset();
				throwHydrantRightTimer.start();
			} else {
				hydrantChangeInY=-.5;
			}
			if (hydrant.hitTestObject(evilDude) && hydrant.x>evilDude.x && hydrant.x<(evilDude.x+evilDude.width/10))
			{
				hitEnemy(50);
				hydrant.x-=20;
				hydrantThrow=0;
			}
		}

		private function changeAlpha(event:TimerEvent):void {
			changeAlphaTimer.reset();
			if (dude.alpha<=0) {
				invisible=false;
				dude.alpha+=.1;
				changeAlphaTimer.start();
			} else if (dude.alpha >= 1) {
				invisible=true;
				dude.alpha-=.1;
				changeAlphaTimer.start();
			} else {
				if (invisible) {
					dude.alpha-=.01;
					if (dude.alpha>0) {
						changeAlphaTimer.start();
					}
				} else {
					dude.alpha+=.01;
					if (dude.alpha<1) {
						changeAlphaTimer.start();
					}
				}
			}
		}
	}
}